package org.xlx.xlx_oa.warehouse.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.common.ResponseBean;
import org.xlx.xlx_oa.warehouse.api.WarehouseFacilityApi;
import org.xlx.xlx_oa.warehouse.domain.vo.FacilityVo;
import org.xlx.xlx_oa.warehouse.service.WarehouseFacilityService;

@RestController
@RequestMapping("/v1/warehouse-facility")
public class WarehouseFacilityController implements WarehouseFacilityApi {
    @Autowired
    private WarehouseFacilityService facilityService;

    @PostMapping("/save")
    @Override
    public ResponseBean saveFacility(@RequestBody FacilityVo facilityVo) {
        return ResponseBean.successFactory(facilityService.saveFacility(facilityVo));
    }

    @DeleteMapping("/remove/{id}")
    @Override
    public ResponseBean removeFacility(@PathVariable(value = "id") Long id) {
        try {
            return ResponseBean.successFactory(facilityService.removeFacility(id));
        } catch (BusinessException b) {
            return ResponseBean.failureFactory(b.getCode(), b.getMessage());
        } catch (Exception e) {
            return ResponseBean.failureFactory(e.getMessage());
        }
    }

    @PostMapping("/list")
    @Override
    public ResponseBean getList(@RequestBody FacilityVo facilityVo) {
        return ResponseBean.successFactory(facilityService.getList(facilityVo));
    }

    @PostMapping("/count")
    @Override
    public ResponseBean getCount(@RequestBody FacilityVo facilityVo) {
        return ResponseBean.successFactory(facilityService.getCount(facilityVo));
    }

    @GetMapping("/detail/{id}")
    @Override
    public ResponseBean getDetail(@PathVariable(value = "id") Long id) {
        return ResponseBean.successFactory(facilityService.getDetail(id));
    }
    @PostMapping("/search")
    @Override
    public ResponseBean searchByTitle(@RequestParam(value = "title") String title) {
        return ResponseBean.successFactory(facilityService.searchByTitle(title));
    }
}

